package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.Log;
import action.club.ActionAddClub;
import action.country.ActionAddCountry;
import action.country.ActionDeleteCountry;
import action.country.ActionGetAllCountries;
import action.country.ActionGetCountry;
import action.country.ActionListCountry;
import action.country.ActionUpdateCountry;
import action.index.ActionIndex;
import action.league.ActionAddLeague;
import action.league.ActionDeleteLeague;
import action.league.ActionGetLeague;
import action.league.ActionListLeague;
import action.league.ActionUpdateLeague;
import action.manager.ActionAddManager;
import action.manager.ActionDeleteManager;
import action.manager.ActionGetManager;
import action.manager.ActionListManager;
import action.manager.ActionUpdateManager;

/**
 * Servlet implementation class FrontController
 */
@WebServlet("*.sp")
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String RequestURI=request.getRequestURI();
		String contextPath=request.getContextPath();
		String command=RequestURI.substring(contextPath.length());
		
		System.out.println("command: "+command);
		
		ActionForward forward=null;
		Action action=null;
		
		try{
			switch(command){
			case "/fbm-admin/index.sp":
				action=new ActionIndex();
				forward=action.execute(request, response);
				break;
				//--------------------Begin Country ----------------------//
			case "/fbm-admin/add-country.sp":
				forward=new ActionForward();
				forward.setPath("add-country.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/add-country-process.sp":
				action=new ActionAddCountry();
				forward=action.execute(request, response);
				break;	
			case "/fbm-admin/country.sp":
				forward=new ActionForward();
				forward.setPath("country.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/list-country-process.sp":
				action=new ActionListCountry();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/delete-country-process.sp":
				action=new ActionDeleteCountry();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/update-country.sp":
				action=new ActionGetCountry();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/update-country-process.sp":
				action=new ActionUpdateCountry();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/get-all-country.sp":
				action=new ActionGetAllCountries();
				forward=action.execute(request, response);
				break;
				//-------------------- End Country --------------------//
				
				//-------------------- Begin League -------------------//
			case "/fbm-admin/add-league.sp":
				forward=new ActionForward();
				forward.setPath("add-league.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/add-league-process.sp":
				action=new ActionAddLeague();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/league.sp":
				forward=new ActionForward();
				forward.setPath("league.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/list-league-process.sp":
				action=new ActionListLeague();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/delete-league-process.sp":
				action=new ActionDeleteLeague();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/update-league.sp":
				action=new ActionGetLeague();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/update-league-process.sp":
				action=new ActionUpdateLeague();
				forward=action.execute(request, response);
				break;
			//--------------------- Begin Manager -----------------//
			case "/fbm-admin/add-manager.sp":
				forward=new ActionForward();
				forward.setPath("add-manager.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/add-manager-process.sp":
				action=new ActionAddManager();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/manager.sp":
				forward=new ActionForward();
				forward.setPath("manager.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/list-manager-process.sp":
				action=new ActionListManager();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/delete-manager-process.sp":
				action=new ActionDeleteManager();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/update-manager.sp":
				action=new ActionGetManager();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/update-manager-process.sp":
				action=new ActionUpdateManager();
				forward=action.execute(request, response);
				break;
			//----------------- End Manager -----------------//
				
			//----------------- Begin Club -----------------//	
			case "/fbm-admin/add-club.sp":
				forward=new ActionForward();
				forward.setPath("add-club.jsp");
				forward.setRedirect(false);
				break;
			case "/fbm-admin/add-club-process.sp":
				action=new ActionAddClub();
				forward=action.execute(request, response);
				break;
			case "/fbm-admin/club.sp":
				forward=new ActionForward();
				forward.setPath("club.jsp");
				forward.setRedirect(false);
				break;
			//------------------- Other ---------------------------//	
			default:
				forward=new ActionForward();
				forward.setPath("/view/404.jsp");
				forward.setRedirect(false);
			}
		}catch(Exception e){
			Log.write(e);
		}
		
		
		//Go
				if(forward != null){
					if(forward.isRedirect()){
						response.sendRedirect(forward.getPath());
					}else{
						RequestDispatcher dispatcher= request.getRequestDispatcher(forward.getPath());
						dispatcher.forward(request, response);
					}
				}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

}
